package com.webapp.services;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;
import com.webapp.DTO.Users;
import com.webapp.interfaces.IRegistrationService;
import com.webapp.utils.HibernateUtil;

@Service
public class RegistrationService implements IRegistrationService {

	public void addUser(Users user){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(user);
		session.getTransaction().commit();
		
		
	}

	public void updataUserData(Users user){
		// TODO Auto-generated method stub
		
	}

	public String getUserLogin(String login) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		String query = "FROM Users as us WHERE us.usLogin = :login";
		Query q = session.createQuery(query);
		q.setString("login", login);
		List<Users> list =(List<Users>) q.list();
		if(list.size() == 1){
			Users user = list.get(0);
			return user.getUsLogin();
		}
		
		return null;
	}

}
